
package com.njrsrc.android.app;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.njrsrc.R;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.utils.Helpers;

/**
 * 设置页面
 * 
 * @author Administrator
 */
public class SettingsActivity extends BaseSampleActivity implements OnClickListener {
    private TitleBar mBar;
    private RelativeLayout logoutLayout;
    private RelativeLayout aboutLayout;
    private TextView logoutTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        initView();
    }

    private void initView() {
        mBar = (TitleBar) findViewById(R.id.activity_title_bar);
        mBar.setTitleText("设置");
        mBar.back.setOnClickListener(this);
        logoutLayout = (RelativeLayout) findViewById(R.id.settings_logout_layout);
        aboutLayout = (RelativeLayout) findViewById(R.id.settings_about_layout);
        logoutTextView = (TextView) findViewById(R.id.settings_logout_tv);
        logoutLayout.setOnClickListener(this);
        aboutLayout.setOnClickListener(this);
        if (!AppApplication.isLogin) {
            logoutTextView.setText(R.string.logoutLogin);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tb_ibtn_back:
                finish();
                break;
            case R.id.settings_logout_layout:
                // 登录或注销
                if (AppApplication.isLogin) {
                    // 已登录用户弹出是否注销
                    Helpers.showDialog(SettingsActivity.this, R.string.logoutTitle,
                            R.string.logoutMessage, new int[] {
                                    R.string.logoutYes, R.string.logoutNo
                            }, new android.content.DialogInterface.OnClickListener[] {
                                    new android.content.DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            // 注销
                                            AppApplication.release();
                                            Editor editor = preferences.edit();
                                            editor.putString("password", "");
                                            editor.commit();
                                            dialog.dismiss();
                                            // 进入登录页面
                                            Intent loginIntent = new Intent();
                                            loginIntent.setClass(SettingsActivity.this,
                                                    LoginActivity.class);
                                            startActivity(loginIntent);
                                            finish();
                                        }
                                    }, new android.content.DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            // 否
                                            dialog.dismiss();
                                        }
                                    }
                            });
                } else {
                    // 进入登录页面
                    Intent loginIntent = new Intent();
                    loginIntent.setClass(SettingsActivity.this, LoginActivity.class);
                    startActivity(loginIntent);
                    finish();
                }
                break;
            case R.id.settings_about_layout:
                // 关于
                startActivity(new Intent(SettingsActivity.this, AboutActivity.class));
                break;

            default:
                break;
        }
    }
}
